package com.cloudcross.ssp.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.SysMail;
import com.cloudcross.ssp.model.SysMailRead;
import com.cloudcross.ssp.service.ISysMailService;

@Service
public class SysMailService implements ISysMailService {

    @Autowired
    private GenericIBatisDao myBatisDao;

    @Override
    public boolean insertSysMail(SysMail sysMail) {
        myBatisDao.save("sysMailSqlMapper.insertSysMail", sysMail);
        return true;
    }

    @Override
    public boolean insertSysMailRead(SysMailRead sysMailRead) {
        myBatisDao.save("sysMailSqlMapper.insertSysMailRead", sysMailRead);
        return true;
    }

    @Override
    public boolean insertSysMailReadBetch(List<Long> sysMaiIdList, Long accountId) {
        myBatisDao.save("sysMailSqlMapper.insertSysMailReadBetch",
                MapBuilder.create(HashMap.class)
                        .add("sysMaiIdList", sysMaiIdList)
                        .add("accountId", accountId).map());
        return true;
    }

    @Override
    public List<SysMail> querySysMailByAccountId(Long accountId, int type, Pager pager) {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("accountId", accountId);
        condition.put("pager", pager);
        if (type == 1) {//后台系统
            return myBatisDao.getList("sysMailSqlMapper.querySysMailListBack", condition);
        } else if (type == 2) {//运营商
            return myBatisDao.getList("sysMailSqlMapper.querySysMailListOperator", condition);
        } else if (type == 3) {//广告主
            return myBatisDao.getList("sysMailSqlMapper.querySysMailListAdvr", condition);
        } else if (type == 4) {//代理商
            return myBatisDao.getList("sysMailSqlMapper", condition);
        }
        return new ArrayList<SysMail>();
    }

    @Override
    public int querySysMailCountByAccountId(Long accountId, int type) {
        if (type == 1) {//后台系统
            return myBatisDao.get("sysMailSqlMapper.querySysMailCountBack", accountId);
        } else if (type == 2) {//运营商
            return myBatisDao.get("sysMailSqlMapper.querySysMailCountOperatorr", accountId);
        } else if (type == 3) {//广告主
            return myBatisDao.get("sysMailSqlMapper.querySysMailCountAdv", accountId);
        } else if (type == 4) {//代理商
            return myBatisDao.get("sysMailSqlMapper", accountId);
        }
        return 0;
    }

    @Override
    public int querySysMailUnRead(Long accountId, int type) {
        if (type == 1) {//后台系统
            return myBatisDao.get("sysMailSqlMapper.querySysMailUnReadBack", accountId);
        } else if (type == 2) {//运营商
            return myBatisDao.get("sysMailSqlMapper", accountId);
        } else if (type == 3) {//广告主
            return myBatisDao.get("sysMailSqlMapper", accountId);
        } else if (type == 4) {//代理商
            return myBatisDao.get("sysMailSqlMapper", accountId);
        }
        return 0;
    }

}
